﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using QuocTeViet.DO;
using QuocTeViet.BL;

namespace QuocTeViet
{
    public partial class Tags : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            load();
        }
        public void load()
        {
            if (Request.QueryString["ID"] != null)
            {
                DataTable dt = new DataTable();
                
                    dt = new cmsSanPhamBL().SelectByTag(Request.QueryString["ID"].ToString(), 0);
                    lblInfo.Text = "Có " + dt.Rows.Count + " sản phẩm có Tag tên <a href='/Tags/" + Request.QueryString["ID"].ToString() + "/default.aspx'><span style='color:red;font-weight:bold;'>" + Request.QueryString["ID"].ToString() + "</span></a>";
               
                bindatalist(dt);
                Page.Title = Request.QueryString["ID"].ToString() + " - " + new sysConfigBL().Select(new sysConfigDO { ConfigID = 12 }).ConfigValue;
            }
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            rptTag.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 10;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rptTag.Visible = true;
                rptTag.DataSource = objPds;
                rptTag.DataBind();
            }
            else
            {
                lbThongbao.Visible = true;
                lbThongbao.Text = "Không có sản phẩm!";
            }
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            load();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            load();
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        public string WordCut(string text)
        {
            return Ultility.WordCut(text, 100, new char[] { ' ', '.', ',', ';' }) + "...";

        }
        public string WordCutTitle(string text)
        {
            return Ultility.WordCut(text, 45, new char[] { ' ', '.', ',', ';' }) + "";

        }
    }
}
